package com.example.nftapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.nftapp.R;
import com.example.nftapp.beans.RmBean;

import java.util.List;

public class RmtjProductAdapter extends RecyclerView.Adapter<RmtjProductAdapter.RmViewHolder> {

    private final List<RmBean> mData;

    public RmtjProductAdapter(List<RmBean> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public RmtjProductAdapter.RmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 将列表项的布局文件转化得到View
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_rv_rm, parent, false);
        return new RmViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RmtjProductAdapter.RmViewHolder holder, int position) {
        // 根据position拿到对应的数据
        RmBean rmBean = mData.get(position);
        // 数据通过ViewHolder设置给布局文件中的组件
        holder.ivImg.setImageResource(rmBean.getImgRes());
        holder.tvName.setText(rmBean.getProName());
        holder.tvStatus.setText(rmBean.getStatus());
        holder.tvXlValue.setText(rmBean.getCount() + "份");
        holder.tvXgValue.setText(rmBean.getLimit() + "份/人");
        holder.tvSeller.setText(rmBean.getSeller());
        holder.tvPrice.setText("￥" + rmBean.getPrice());
        if (rmBean.getStatus().startsWith("敬请期待")) {
            holder.tvStatus.setBackgroundResource(R.drawable.bg_rmtj_status_blue);
        } else if (rmBean.getStatus().startsWith("即将开售")) {
            holder.tvStatus.setBackgroundResource(R.drawable.bg_rmtj_status_green);
        } else if (rmBean.getStatus().startsWith("正在发行")) {
            holder.tvStatus.setBackgroundResource(R.drawable.bg_rmtj_status_orange);
        } else {
            holder.tvStatus.setBackgroundResource(R.drawable.bg_rmtj_status_gray);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class RmViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImg;
        TextView tvName;
        TextView tvStatus;
        TextView tvXlValue;
        TextView tvXgValue;
        TextView tvSeller;
        TextView tvPrice;

        public RmViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImg = itemView.findViewById(R.id.ivImg);
            tvName = itemView.findViewById(R.id.tvName);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            tvXlValue = itemView.findViewById(R.id.tvXlValue);
            tvXgValue = itemView.findViewById(R.id.tvXgValue);
            tvSeller = itemView.findViewById(R.id.tvSeller);
            tvPrice = itemView.findViewById(R.id.tvPrice);
        }
    }

}
